package com.example.lemieripetizioni;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.lemieripetizioni.adapter.ProfessoreListAdapterWithCache;
import com.lemieripetizioni.datamodel.Professore;
import com.lemieripetizioni.datamodel.Ripetizione;
import com.lemieripetizioni.dbadapter.DbAdapter;

public class CercaIlProfFragment extends Fragment {
	private DbAdapter dbHelper;
	private Cursor cursor;
	private Context ctx;
	List<Professore> listProf;

	private static final String TAG = "CercaIlProf - ";

	private EditText string_search;
	private ListView listViewProf;

	ProfessoreListAdapterWithCache professoreListAdapterWithCache;

	int textlength = 0;

	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.cerca_il_prof, container, false);

		ctx = getActivity();

		listProf = new ArrayList<Professore>();

		string_search = (EditText) rootView.findViewById(R.id.cerca_il_prof_edit_search);

		Date currDate = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currDate);
		calendar.add(Calendar.MONTH, 1);

		// get all prof
		getAllProf();

		listViewProf = (ListView) rootView.findViewById(R.id.cerca_il_prof_list_result);
		professoreListAdapterWithCache = new ProfessoreListAdapterWithCache(ctx, R.layout.cerca_il_prof_list_result_item, listProf, this.getActivity());

		listViewProf.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Professore profSelected = professoreListAdapterWithCache.getItem(position);
				Intent i = new Intent(ctx, ProfessoreDetail.class);
				i.putExtra("prof", profSelected);
				ctx.startActivity(i);

			}
		});

		string_search.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				textlength = string_search.getText().length();
				List<Professore> localSearch = new ArrayList<Professore>();

				for (int i = 0; i < listProf.size(); i++) {
					if (listProf.get(i).getNome().toLowerCase().contains(string_search.getText().toString().toLowerCase().trim())) {
						localSearch.add(listProf.get(i));

					}
				}
				listViewProf.setAdapter(new ProfessoreListAdapterWithCache(ctx, R.layout.cerca_il_prof_list_result_item, localSearch, getActivity()));
			}
		});

		listViewProf.setAdapter(professoreListAdapterWithCache);

		return rootView;
	}

	private void getAllProf() {
		listProf.clear();
		dbHelper = new DbAdapter(this.getActivity());
		dbHelper.open();

		// TODO da rimuovere mock per inserimento professori in lista

		mockInsertProfessoreList();

		cursor = dbHelper.fetchAllProfEntry();

		while (cursor.moveToNext()) {

			String contactID = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_CONTACTID));
			String urlFoto = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_URL_FOTO));
			String nome = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_NOME));
			String cognome = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_COGNOME));
			String dataNascita = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_DATA_NASCITA));
			String materiaInsegnata = cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_MATERIA_INSEGNATA));
			Professore p = new Professore(urlFoto, nome, cognome, dataNascita, materiaInsegnata);
			Log.i(TAG, p.toString());

			listProf.add(p);
		}

		dbHelper.close();
		cursor.close();
	}

	private void mockInsertProfessoreList() {
		Professore prof_mock1 = new Professore("/storage/sdcard0/Download/ragazza_carina_libri.jpeg", "Marco", "Polo", "01/01/1970", "Matematica");
		Professore prof_mock2 = new Professore("/storage/sdcard0/Download/ragazza_carina_libri.jpeg", "valentino", "Rossi", "01/01/1970", "Fisica");
		Professore prof_mock3 = new Professore("/storage/sdcard0/Download/ragazza_carina_libri.jpeg", "Marco", "Van Basten", "01/01/1970", "Matematica");

		dbHelper.createProfEntry(prof_mock1);
		dbHelper.createProfEntry(prof_mock2);
		dbHelper.createProfEntry(prof_mock3);
	}
}